<?php
require_once __DIR__.'/../../lib/PAutoLoader.php';
use Common\FileUtil;
$config = include __DIR__.'/../../config/thrift_ini.php';
// print_r($config);

    function loadThriftService($config){
//         print_r($config);
//         exit();
        $genDir = $config['gen-php'];
        $handlerDir = $config['handler'];
        echo $genDir ."==\n";
        $genFiles = FileUtil::getPathFiles($genDir);
        print_r($genFiles);
        $handlerFiles = FileUtil::getPathFiles($handlerDir);
        print_r($handlerFiles);
        $handlerClasses = array();
        if(count($handlerFiles)){
            foreach ($handlerFiles as $file){
                require $file;
                //echo "1=".$file."\n";
                $fileClass = substr($file, strlen($handlerDir), strlen($file) - strlen($handlerDir) - 4);
                //echo "2=".$fileClass."\n";
                $fileInfo = pathinfo($file);
                $className = $fileInfo['filename'];
                $handlerClasses[$className] = $fileClass;
            }
        }
        print_r($handlerClasses);
        if(count($genFiles) > 0){
            foreach($genFiles as $file){
                require $file;
                $service_class = substr($file, strlen($genDir),  strlen($file) - strlen($genDir) - 4);
                $info = pathinfo($file);
                $serviceName = $info['filename'];
                $processor_class = $service_class . 'Processor';
                $handler_class = $handlerClasses[$className.'Handler'];
                $handler = new $handler_class();
                $processor = new $processor_class($handler);
                //$tMultiplexedProcessor->registerProcessor($serviceName, $processor);
//                 class_exists($class_name)
            }
        }
        
        
//         foreach ($services as $serviceName => $service) {
//             $service_class = $service['service'];
//             $processor_class = $service_class . 'Processor';
//             $handler_class = $service['handler'];
//             $handler = new $handler_class();
//             $processor = new $processor_class($handler);
//             $this->services[$serviceName] = $processor;
//             //$tMultiplexedProcessor->registerProcessor($serviceName, $processor);
//         }
    }

loadThriftService($config);